(function(avalon, win){
	var vm;
	document.addEventListener('touchmove', function(event) {
        if (event.target.type == 'range') return;
        event.preventDefault();
    });
	(function() {
        var root = $('html');
        $(win).resize(getSize).resize();
        function getSize() {
            win.rootSize = document.body.clientWidth / 16;
            if(rootSize > 80) rootSize = 80;
            root.css({
                'fontSize': rootSize,
                'visibility': 'visible'
            });
        }
    })();
	var date = new Date();
	avalon.component('ms:date', {
		weekTexts: ['一', '二', '三', '四', '五', '六', '日'],
		bodyHeight: $(window).height() - 4.8 * rootSize,
		ovHeight: 0,
		todayText: '今',
		itemWidth: 16 / 7,
		thisDate: {
			year: date.getFullYear(), //年
			month: date.getMonth() + 1,//月
			tdate: date.getDate(), //日
			weekDay: date.getDay()//星期
		},
		currentDate: {
			year: date.getFullYear(), //年
			month: date.getMonth() + 1,//月
			tdate: date.getDate(), //日
			weekDay: date.getDay()//星期
		},
		daysArray: [],
		$template: document.getElementById('tp').value,
		$init: function(initVm){
			vm = initVm;
			vm.changeMonth(initVm.thisDate.year, initVm.thisDate.month);
		},
		changeMonth: function(year, month){
			vm.daysArray = setMonthDays(year, month);
			if(vm.onRefreshRendArr) {
				vm.onRefreshRendArr(vm.daysArray, year+'-'+month);
			}
			vm.onMonthChange(year, month);
			vm.ovHeight = ($(window).height() - 4.8 * rootSize) / Math.ceil(vm.daysArray.length / 7);
		},
		prevMonth: function(){
			var date = getPrevMonth(vm.currentDate.year, vm.currentDate.month);
			vm.changeMonth(date.year, date.month);
		},
		nextMonth: function(){
			var date = getNextMonth(vm.currentDate.year, vm.currentDate.month);
			vm.changeMonth(date.year, date.month);
		},
		onMonthChange: function(){},
		onRefreshRendArr: null
	});

	function getPrevMonth(year, month){
		if(month === 1){
			return {
				year: year - 1,
				month: 12
			};
		} else {
			return {
				year: year,
				month: month - 1
			};
		}
	}

	function getNextMonth(year, month){
		if(month === 12){
			return {
				year: year + 1,
				month: 1
			};
		} else {
			return {
				year: year,
				month: month + 1
			};
		}
	}

	function setMonthDays(year, month){
		var monthArray = [];
		var emptyMonthDay = new Date(year + '/' + month + '/' + '01').getDay() - 1;
		var monthLength = getMonthLength(year, month);
		for(var i = 0; i < emptyMonthDay; i ++ ){
			monthArray.push({});
		}
		for(var i = 0; i < monthLength; i ++) {
			monthArray.push({
				day: i + 1,
				today: year === vm.thisDate.year && month === vm.thisDate.month && vm.thisDate.tdate === i + 1,
				start: false,
				end: false
			});
		}
		vm.currentDate.year = year;
		vm.currentDate.month = month;
		return monthArray;
	}
	//获取本月长度
	function getMonthLength(year, month){
		var isRun = year % 4 === 0;
		if([1,3,5,7,8,10,12].indexOf(month) > -1) return 31;
		if([4,6,9,11].indexOf(month) > -1) return 30;
		return isRun ? 29 : 28;
	}
})(avalon, window);